package com.ljy.my_study.leetcode.powx_n;

/**
 * @author lijunying
 * @date 2021/9/6 11:34
 */
public class TestMain2 {
//    注意：n要转成long以防“-n”超出范围
    public double myPow(double x, int n) {
        long N=n;
        if(N<0){
            return 1/pow(x,-N);
        }
        return pow(x,N);
    }

    private double pow(double x,long n){
        if(n==0){
            return 1;
        }
        double num=pow(x*x,n/2);
        if(n%2==1){
            num=num*x;
        }
        return num;
    }
}
